import java.util.*;

public class AnagramSolver 
{
	//fields
	List<String[]> anagrams;
	List<String> abridged;
	List<String> dictionary;
	Map<String, LetterInventory> dictInv;
	
	public AnagramSolver(List<String> list)
	{
		dictionary = new LinkedList<String>();
		dictionary.addAll(list);
		dictInv = new HashMap<String, LetterInventory>();
		for(String word : list)
		{
			LetterInventory curWord = new LetterInventory(word);
			dictInv.put(word, curWord);
		}
		anagrams = new LinkedList<String[]>();
	}
	
	public void print(String phrase, int max)
	{
		LetterInventory phraseInv = new LetterInventory(phrase);
		String[] results = new String[3];
		generate(phraseInv, results);
		for(String result[] : anagrams)
		{
			//System.out.println(Arrays.toString(result));
		}
	}
	
	private void generate(LetterInventory phraseInv, String[] results)
	{
		abridged = new LinkedList<String>();
		reduceDict(phraseInv);
		int i = 0;
		for(String word : abridged)
		{
			results[i] = word;
			LetterInventory subset = phraseInv.subtract(dictInv.get(word));
			generate(subset, results);
			i++;
		}
		anagrams.add(results);
	}
	
	private void reduceDict(LetterInventory phraseInv)
	{
		for(String word : dictionary)
		{
			if(phraseInv.subtract(dictInv.get(word)) != null)
			{
				abridged.add(word);
			}
		}
	}
}
